package com.puti.doctor.controller;



import java.math.BigDecimal;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.puti.common.core.domain.R;
import com.puti.doctor.domian.HospitalDoctorInfo;
import com.puti.doctor.domian.PatientInfo;
import com.puti.doctor.service.IHospitalDoctorInfoService;
import com.puti.doctor.service.IPatientInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.puti.common.log.annotation.Log;
import com.puti.common.log.enums.BusinessType;
import com.puti.common.core.web.controller.BaseController;
import com.puti.common.core.web.domain.AjaxResult;
import com.puti.common.core.utils.poi.ExcelUtil;
import com.puti.common.core.web.page.TableDataInfo;

/**
 * 患者-个人信息 患者-个人信息Controller
 * 
 * @author puti
 * @date 2021-04-23
 */
@CrossOrigin
@RestController
@RequestMapping("/sick")
public class PatientInfoController extends BaseController
{
    @Autowired
    private IPatientInfoService patientInfoService;
    @Autowired
    private IHospitalDoctorInfoService hospitalDoctorInfoService;




    /**
     * 查询患者-个人信息 患者-个人信息列表
     */

    @GetMapping("/list")
    public TableDataInfo list(PatientInfo patientInfo)
    {
        startPage();
        List<PatientInfo> list = patientInfoService.selectPatientInfoList(patientInfo);
        return getDataTable(list);
    }

    /**
     * 导出患者-个人信息 患者-个人信息列表
     */
    @Log(title = "患者-个人信息 患者-个人信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PatientInfo patientInfo) throws IOException
    {
        List<PatientInfo> list = patientInfoService.selectPatientInfoList(patientInfo);
        ExcelUtil<PatientInfo> util = new ExcelUtil<PatientInfo>(PatientInfo.class);
        util.exportExcel(response, list, "患者-个人信息 患者-个人信息数据");
    }

    /**
     * 获取患者-个人信息 患者-个人信息详细信息
     */
    @GetMapping("/getInfo/{patientId}")
    public R getInfo(@PathVariable("patientId") Long patientId)
    {
        return R.ok(patientInfoService.selectPatientInfoById(patientId),"查询成功!");
    }

    /**
     * 新增患者-个人信息 患者-个人信息
     */
    @Log(title = "患者-个人信息 患者-个人信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PatientInfo patientInfo)
    {
        return toAjax(patientInfoService.insertPatientInfo(patientInfo));
    }

    /**
     * 修改患者-个人信息 患者-个人信息
     */
//    @Log(title = "患者-个人信息 患者-个人信息", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody PatientInfo patientInfo)
//    {
//        return toAjax(patientInfoService.updatePatientInfo(patientInfo));
//    }

    @Log(title = "患者-个人信息 患者-个人信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(Long patientId,Long doctorStart)
    {
        if (doctorStart>5){
            return R.ok("不能给5颗星以上");
        }
        PatientInfo info = patientInfoService.selectPatientInfoById(patientId);
        HospitalDoctorInfo doctorInfo = hospitalDoctorInfoService.selectHospitalDoctorInfoById(info.getPatientDoctorId());
        info.setPatientPay(new BigDecimal(doctorStart*10));
        doctorInfo.setDoctorMoney(new BigDecimal(doctorStart*10));
        doctorInfo.setDoctorStart(doctorStart);
        hospitalDoctorInfoService.updateHospitalDoctorInfo(doctorInfo);
        return R.ok(patientInfoService.updatePatientInfo(info),"支付成功");
    }

//    @Log(title = "患者-个人信息 患者-个人信息", businessType = BusinessType.UPDATE)
    //快速问诊-问诊状态：限制医生接诊人数为5，显示当前问诊已满请等待，问诊为4人可咨询状态；
    @PutMapping("/admission")
    public R editt(Long patientId)
    {
        PatientInfo info = patientInfoService.selectPatientInfoById(patientId);
        HospitalDoctorInfo doctorInfo = hospitalDoctorInfoService.selectHospitalDoctorInfoById(info.getPatientDoctorId());
        doctorInfo.setDoctorAdmissionStatus(1L);
        if (doctorInfo.getDoctorInquiryStatus()>5){
            return R.ok("当前问诊已满请等待");
        }
        doctorInfo.setDoctorInquiryStatus(doctorInfo.getDoctorInquiryStatus()+1);

        return R.ok(hospitalDoctorInfoService.updateHospitalDoctorInfo(doctorInfo),"当前问诊人数为"+doctorInfo.getDoctorInquiryStatus()+",可以咨询,j加入问诊成功");
    }
    /**
     * 删除患者-个人信息 患者-个人信息
     */
    @Log(title = "患者-个人信息 患者-个人信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{patientIds}")
    public AjaxResult remove(@PathVariable Long[] patientIds)
    {
        return toAjax(patientInfoService.deletePatientInfoByIds(patientIds));
    }
}
